<?php
namespace myclass;
class Sku
{
    private $shoptype_num;
    private $shoptype;
    private $db;
    private $f_name;
    public  $category;
    public  $category_ids;

    function setShoptypeNum($shoptype_num){
        switch ($shoptype_num){
            case "1":
                $shoptype="shate";
                break;
            case "2":
                $shoptype="douyin";
                break;
            case "3":
                $shoptype="weixin";
                break;
            case "4":
                $shoptype="shopee";
                break;
        }
        $this->shoptype($shoptype);
    }
    function shoptype($shoptype){
        $this->shoptype=$shoptype;
        switch ($shoptype){
            case "douyin":
                $shoptype_num=2;
                $db="jipu.jipu_item_category_dy";
                $f_name="_dy";
                break;
            case "weixin":
                $shoptype_num=3;
                $db="jipu.jipu_item_category_sph";
                $f_name="_sph";
                break;
            case "shopee":
                $shoptype_num=4;
                $db="jipu.jipu_item_category_shopee";
                $f_name="_xiap";
                break;
            case "shante":
                $shoptype_num=1;
                $db="jipu.jipu_item_category";
                $f_name="";
                break;
        }
        $this->db=$db;
        $this->f_name=$f_name;
        $this->shoptype_num=$shoptype_num;
    }

    /**
     * 给map数据表中对应的shoptype插入一个$category,并处理其他相关字段
     * @param $category
     * @param $mapId
     * @return int|mixed|string
     */
    function clMapCategory($category="",$mapId=""){
        if($category!="") {
            $data = array();
            $data["category_{$this->shoptype_num}"] = $category;
            $mapId = insertupdate("jipu.jipu_item_category_map", $data, "select id from jipu.jipu_item_category_map where category_{$this->shoptype_num}='{$category}'");
        }else{
            $category=getone("select category_{$this->shoptype_num} from jipu.jipu_item_category_map where id='{$mapId}'");
        }
        if($category==""){
            $category_row=getrow("select category_1,category_2,category_3 from jipu.jipu_item_category_map where id='{$mapId}'");
            if($category_row["category_{$this->shoptype_num}"]!="") return;
            for ($i = 1; $i <= 4; $i++) {
                if($category_row["category_{$i}"]!=""){
                    $category_qian=preg_replace("/^(.*)(>.*)/","\\1",$category_row["category_{$i}"]);
                    $shoptype_num_2=$i;
                    $category_row_2=getrow("select * from jipu.jipu_item_category_map where category_{$shoptype_num_2} like '{$category_qian}%' and status=1");
                    $data = array();
                    for ($n = 1; $n <= 4; $n++) {
                        if($n==$i) continue;
                        $category=preg_replace("/^(.*)(>.*)/","\\1",$category_row_2["category_{$n}"]);
                        $data["category_{$n}"] = $category;
                        ${"category_{$n}_ids"}=preg_replace("/^(.*)(\,.*)/","\\1",$category_row_2["category_{$n}_ids"]);
                        $data["category_{$n}_ids"] = ${"category_{$n}_ids"};
                        $nn=explode(",",$data["category_{$n}_ids"]);
                        $sku3=new Sku();
                        $sku3->setShoptypeNum($n);
                        for ($j = 0; $j < count($nn); $j++) {
                            $data["cid_".($j+1).$sku3->f_name]=$nn[$j];
                        }
                    }
                    $data['status']=0;
                    insertupdate("jipu.jipu_item_category_map", $data, "select id from jipu.jipu_item_category_map where id='{$mapId}'");
                    return;
                }
            }
            if($category=="") return 0;
        }

        $category=str_replace(" ","",$category);
        $nn=explode(">",$category);
        $sql_qian="select id from $this->db where 1=1";
        $id=0;
        $ids=[];
        for ($i = 0; $i < count($nn); $i++) {
            $sql=$sql_qian;
            if($id<1){
                $sql.=" and pid=0";
            }else{
                $sql.=" and pid={$id}";
            }
            $sql.=" and name='{$nn[$i]}'";
            $id=getone($sql);
            $ids[]=$id;
            execute("update jipu.jipu_item_category_map set cid_".($i+1)."{$this->f_name}='{$id}' where id={$mapId}");
        }
        execute("update jipu.jipu_item_category_map set category_".$this->shoptype_num."_ids='".join(",",$ids)."' where id={$mapId}");
        return $mapId;
    }


    /**
     * 处理map表的其他shoptype
     * @return void
     */
    function clMapOtherShoptypeCategory($mapId){
        $shops=array("shante","douyin","weixin","shopee");
        for ($i = 0; $i < count($shops); $i++) {
            if($shops[$i]!=$this->shoptype){
                $sku2=new Sku();
                $sku2->shoptype($shops[$i]);
                $sku2->clMapCategory("",$mapId);
            }
        }
    }

    function updateItemCategoryFromMap($item_id,$mapId){
        $rs=getrow("select * from jipu.jipu_item_category_map where id='{$mapId}'");
        $cs="category_1,category_2,category_3,category_4,cid_1,cid_2,cid_3,cid_1_dy,cid_2_dy,cid_3_dy,cid_4_dy,cid_5_dy,cid_1_sph,cid_2_sph,cid_3_sph,cid_4_sph,cid_5_sph,cid_1_xiap,cid_2_xiap,cid_3_xiap,cid_4_xiap";
        $nn=explode(",",$cs);
        $data=array();
        for ($i = 0; $i < count($nn); $i++) {
            if($rs[$nn[$i]]!="null"&&$rs[$nn[$i]]!=""){
                if($nn[$i]=="category_1") {
                    $field_name = "category";
                }elseif($nn[$i]=="category_2"){
                    $field_name="category2";
                }elseif($nn[$i]=="category_3"){
                    $field_name="category3";
                }elseif($nn[$i]=="category_4"){
                    $field_name="category4";
                }else{
                    $field_name=$nn[$i];
                }
                $data[$field_name]=$rs[$nn[$i]];
            }
        }
        $this->category=$rs['category_1'];
        $this->category_ids=$rs['category_1_ids'];
        insertupdate("jipu.jipu_item",$data,"select id from jipu.jipu_item where id='{$item_id}'");
    }

    function updateItemCategoryFromMapShante($item_id){
        $c=getrow("select cid_1,cid_2,cid_3 from jipu.jipu_item where id='{$item_id}'");
        $category="";
        if($c['cid_1']>0){
            $category=$this->categoryName($c['cid_1']);
        }
        if($c['cid_2']>0){
            $category.="/".$this->categoryName($c['cid_2']);
        }
        if($c['cid_3']>0){
            $category.="/".$this->categoryName($c['cid_3']);
        }
        execute("update jipu.jipu_item set category='{$category}' where id='{$item_id}'");
        $category=str_replace("/",">",$category);
        $mapId=getone("select id from jipu.jipu_item_category_map where category_1='{$category}'");
        if($mapId!=""){
            $this->updateItemCategoryFromMap($item_id,$mapId);
        }
    }

    function categoryName($id){
        return getone("select name from jipu.jipu_item_category where id='{$id}'");
    }
}

